import { Controller } from "../../mvc/core/Controller";
import { DramaMapConfig } from "../resource/DramaMapConfig";
import { DataEvent } from "../events/DataEvent";
import { Lv70DramaChoosePanel } from "../view/lv70fb/win/Lv70DramaChoosePanel";
import { Lv70DramaMapMain } from "../view/lv70fb/map/Lv70DramaMapMain";
import { Lv70DramaIconBtn } from "../view/lv70fb/Lv70DramaIconBtn";
import { Lv70DramaStepOperator } from "../view/lv70fb/Lv70DramaStepOperator";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { GameProxy } from "../mvc/GameProxy";
import { EDelegateKind } from "../../../Message/Game/EDelegateKind";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ClockManager } from "../manager/ClockManager";
import { TimeEvent } from "../events/TimeEvent";
import { DramaMultipleDelegatePanel } from "../view/delegate/dramaDelegate/DramaMultipleDelegatePanel";
import { Global } from "../../../com/gengine/global/Global";
import { Log } from "../../../com/gengine/debug/Log";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { LoaderHelp } from "../../common/display/LoaderHelp";
import { ResFileConst } from "../resource/ResFileConst";
import { SystemSetter } from "../view/system/SystemSetter";
import { Lv70DramaConfig } from "../resource/Lv70DramaConfig";
import { Lv70DramaDifficulty } from "../view/lv70fb/data/Lv70DramaDifficulty";
import { TDramaCopy } from "../../../Message/Db/Tables/TDramaCopy";
import { TDrama } from "../../../Message/Db/Tables/TDrama";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { SSeqPlayerItem } from "../../../Message/Public/SSeqPlayerItem";
import { SceneRange } from "../scene/map/SceneRange";
import { GameController } from "../mvc/GameController";
import { LoaderManager } from "../../../com/gengine/resource/LoaderManager";
import { LoaderPriority } from "../../../com/gengine/resource/LoaderPriority";
import { ResourceInfo } from "../../../com/gengine/resource/info/ResourceInfo";
import { Game } from "../Game";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
type int = number;
//class Lv70DramaController
    
    export  class Lv70DramaController extends Controller
    {
        constructor()
        {
            super();this.codesForPreLoad = {"302":"\t90055.swf", "402":"90055.swf"};
            
            this.CurChapterNum = DramaMapConfig.instance.maxChapterNum;
            return;
        }

        /* internal  */onDelegateWarehouseItemsUpdate(arg1: DataEvent): void
        {
            if (this._map) 
            {
                this._map.checkWareHouseItems();
            }
            return;
        }

        /* internal  */get winChoose(): Lv70DramaChoosePanel
        {
            if (this._winChoose == null) 
            {
                this._winChoose = new Lv70DramaChoosePanel();
            }
            return this._winChoose;
        }

        /* internal  */get map(): Lv70DramaMapMain
        {
            if (this._map == null) 
            {
                this._map = new Lv70DramaMapMain(this.CurChapterNum);
            }
            return this._map;
        }

        /* internal  */get icon(): Lv70DramaIconBtn
        {
            if (this._icon == null) 
            {
                this._icon = new Lv70DramaIconBtn();
            }
            return this._icon;
        }

        /* internal  */get stepOperator(): Lv70DramaStepOperator
        {
            if (this._stepOperator == null) 
            {
                this._stepOperator = new Lv70DramaStepOperator();
            }
            return this._stepOperator;
        }

        /* internal  */nextDayCopyInfoGotHandler(): void
        {
            this.icon.show();
            return;
        }

        /* internal  */onActiveDramaRewardHandler(arg1: SActiveToClient): void
        {
            if (this._icon && !this._icon.isHide) 
            {
                this._icon.updateActiveToClientInfo(arg1);
            }
            return;
        }

        /* internal  */onDelegateDramaCopyHandler(): void
        {
            GameProxy.delegateTask.dramaDelegate(this._dramaCode, EDelegateKind._EDelegateKindFree, 1);
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.Lv70DramaGetBonus, this.getBonusHandler);
            NetDispatcher.addCmdListener(ServerCommand.Lv70DramaNextStepUpdate, this.nextStepHandler);
            NetDispatcher.addCmdListener(ServerCommand.Lv70DramaInfoGot, this.dramaInfoGotHandler);
            NetDispatcher.addCmdListener(ServerCommand.Lv70DramaEnterSuccess, this.enterDramaSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.Copy_LeaveCopy, this.onLeaveCopyHandler);
            NetDispatcher.addCmdListener(ServerCommand.Lv70DramaStepSuccess, this.stepSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveDramaReward, this.onActiveDramaRewardHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaStartNextStep, this.startNextStepHandler);
            Dispatcher.addEventListener(EventName.DramaMoveFinished, this.dramaAiMoveFinishedHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaChooseDrama, this.openChooseDramaHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaEnterDrama, this.enterDramaHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaLeaveDrama, this.leaveDramaHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaSkipDrama, this.skipDramaHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaMapTabChange, this.mapTabChangeHandler);
            Dispatcher.addEventListener(EventName.Lv70DramaMapDifficultyChanged, this.difficultyChanged);
            Dispatcher.addEventListener(EventName.DelegateDramaCopy, this.onDelegateDramaCopy);
            Dispatcher.addEventListener(EventName.DoDelegateDramaCopy, this.doDelegateDramaCopy);
            Dispatcher.addEventListener(EventName.Guide_CopyStoryGuide, this.onLv70DramaTaskHandler);
            NetDispatcher.addCmdListener(ServerCommand.DelegateWarehouseItemsUpdate, this.onDelegateWarehouseItemsUpdate);
            ClockManager.instance.addEventListener(TimeEvent.DateChange, this.nextDayHandler);
            return;
        }

        /* internal  */nextDayHandler(arg1: Object=null): void
        {
            flash.utils.setTimeout(this.nextDayTimerOut, 3000);
            return;
        }

        /* internal  */nextDayTimerOut(): void
        {
            GameProxy.copy.getDramaCopyInfo(this.nextDayCopyInfoGotHandler);
            return;
        }

        /* internal  */showActiceMultipPanel(): void
        {
            if (!this._dramaMultipPanel) 
            {
                this._dramaMultipPanel = new DramaMultipleDelegatePanel(this.map);
            }
            this._dramaMultipPanel.show(276, 190);
            return;
        }

        /* internal  */startNextStepHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=int(arg1.data);
            if (Global.isDebugModle) 
            {
                loc2 = "70级副本前端向服务器请求前端已经完成了步骤:" + loc1 + ",  现在的时间是:" + flash.utils.getTimer();
                Log.debug(loc2);
            }
            GameProxy.copy.doNextDrama(loc1);
            return;
        }

        /* internal  */dramaAiMoveFinishedHandler(arg1: DataEvent): void
        {
            this.stepOperator.onDramaAiFinshed();
            return;
        }

        /* internal  */doDelegateDramaCopy(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.delegateTask.dramaDelegate(this._dramaCode, EDelegateKind._EDelegateKindFree, loc1);
            return;
        }

        /* internal  */openChooseDramaHandler(arg1: DataEvent): void
        {
            if (cache.copy.isInLv70DramaCopy()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80483));
                return;
            }
            if (this._isResFileGot) 
            {
                this.onResLoadHandler();
            }
            if (this._isLoading) 
            {
                return;
            }
            this._isLoading = true;
            GameProxy.copy.getDramaCopyInfo(this.onGotDramaInfo);
            return;
        }

        /* internal  */onGotDramaInfo(): void
        {
            LoaderHelp.addResCallBack(ResFileConst.Lv70DramaMapMain, this.onLoadMapMain);
            return;
        }

        /* internal  */onLoadMapMain(): void
        {
            LoaderHelp.addResCallBack(ResFileConst.lv70Drama, this.onResLoadHandler);
            return;
        }

        /* internal  */onResLoadHandler(): void
        {
            this._isResFileGot = true;
            this._isLoading = false;
            if (Global.isDebugModle) 
            {
                this.winChoose.show();
            }
            this.map.updateTodayDoneTimes(Lv70DramaController.MaxPassTimes - cache.copy.getLv70DramaTodayDoneTimes());
            this.map.show();
            this.setDefaultBehavior();
            this.setDefaultView();
            return;
        }

        /* internal  */setDefaultBehavior(): void
        {
            var loc1=false;
            var loc2=SystemSetter.localSetter.lv70DramaChapter;
            var loc3=SystemSetter.currentSetter.lv70DramaDifficulty;
            var loc4=Lv70DramaConfig.instance.getLastDramaCopyByChapter(loc2, loc3);
            var loc5=Lv70DramaConfig.instance.getChapterNum(loc3);
            if (SystemSetter.currentSetter.lv70DramaDifficulty != Lv70DramaDifficulty.None) 
            {
                if (!(loc4 == null) && loc4.copyCode == cache.copy.getLv70HightesHistory(loc3)) 
                {
                    if (loc2 < loc5) 
                    {
                        SystemSetter.localSetter.lv70DramaChapter = loc2 + 1;
                        loc1 = true;
                    }
                }
            }
            else 
            {
                if (parseInt(cache.role.combatCapabilities) >= this.MinCombatForDifficult) 
                {
                    SystemSetter.localSetter.lv70DramaDifficulty = Lv70DramaDifficulty.Difficult;
                }
                else 
                {
                    SystemSetter.localSetter.lv70DramaDifficulty = Lv70DramaDifficulty.Easy;
                }
                loc1 = true;
            }
            if (loc1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            }
            return;
        }

        /* internal  */setDefaultView(): void
        {
            if (this._map == null) 
            {
                return;
            }
            this.changeToTabMap(SystemSetter.localSetter.lv70DramaChapter);
            return;
        }

        /* internal  */enterDramaHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["config"] as TDramaCopy;
            this._askInDramaCopy = loc1;
            var loc2=Boolean(arg1.data["isDrama"]);
            GameProxy.copy.enterDramaCopy(loc1.copyCode, loc2);
            return;
        }

        /* internal  */leaveDramaHandler(arg1: DataEvent): void
        {
            GameProxy.copy.leftGroup_async();
            return;
        }

        /* internal  */skipDramaHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as TDrama;
            this.stepOperator.skipDrama(loc1);
            return;
        }

        /* internal  */mapTabChangeHandler(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            this.changeToTabMap(loc1);
            return;
        }

        /* internal  */difficultyChanged(arg1: DataEvent): void
        {
            var loc1=int(arg1.data);
            SystemSetter.localSetter.lv70DramaDifficulty = loc1;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            this.map.updateMap();
            return;
        }

        /* internal  */onDelegateDramaCopy(arg1: DataEvent): void
        {
            if (cache.copy.isInLv70DramaCopy()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69717));
                return;
            }
            var loc1=arg1.data as TDramaCopy;
            if (loc1) 
            {
                this._dramaCode = loc1.copyCode;
                if (cache.active.isActiveInEffect(EActiveType._EActiveTypeDramaMultiDrop)) 
                {
                    this.showActiceMultipPanel();
                    this._dramaMultipPanel.updateCost(cache.active.getActiveByCode(EActiveType._EActiveTypeDramaMultiDrop));
                }
                else 
                {
                    this.onDelegateDramaCopyHandler();
                }
            }
            return;
        }

        /* internal  */changeToTabMap(arg1: int): void
        {
            if (arg1 > this.CurChapterNum) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70077));
                this.map.changeTab(this._lastTab);
                return;
            }
            this._lastTab = arg1;
            this.map.changeTab(arg1);
            this.map.changeToMap(arg1);
            SystemSetter.localSetter.lv70DramaChapter = arg1;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
            return;
        }

        /* internal  */getBonusHandler(arg1: Object): void
        {
            var loc1=arg1 as SSeqPlayerItem;
            this.stepOperator.showBonus(loc1);
            return;
        }

        /* internal  */dramaInfoGotHandler(arg1: Object): void
        {
            if (this.hasTodayDone()) 
            {
                this.recycleMe();
            }
            if (this._map != null) 
            {
                this._map.updateTodayDoneTimes(Lv70DramaController.MaxPassTimes - cache.copy.getLv70DramaTodayDoneTimes());
            }
            this.setDefaultBehavior();
            this.setDefaultView();
            return;
        }

        /* internal  */recycleMe(): void
        {
            if (this._icon != null) 
            {
                this._icon.hide();
                this._icon = null;
            }
            if (this._stepOperator != null) 
            {
                this._stepOperator.dispose();
                this._stepOperator = null;
            }
            if (this._map != null) 
            {
                this._map.hide();
                this._map.dispose();
                this._map = null;
            }
            return;
        }

        /* internal  */enterDramaSuccessHandler(arg1: Object): void
        {
            this.stepOperator.restart();
            SceneRange.cancelNoMoveWH(true);
            GameController.gameui.getGameui().showAllUI(false);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            Lv70DramaConfig.instance.curCopy = this._askInDramaCopy;
            this.map.hide();
            this.winChoose.hide();
            this.preLoad();
            cache.guide.guideCopyStory = false;
            if (this._icon != null) 
            {
                this.icon.showEffect(false);
                this.icon.showGuidArrow(false);
            }
            return;
        }

        /* internal  */preLoad(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc6=0;
            var loc7=this.codesForPreLoad;
            for (loc1 in loc7) 
            {
                if (this._askInDramaCopy.copyCode.toString() != loc1) 
                {
                    continue;
                }
                loc2 = this.codesForPreLoad[loc1] as string;
                loc3 = loc2.split(",");
                loc4 = 0;
                while (loc4 < loc3.length) 
                {
                    if (!((loc5 = loc3[loc4] as string) == null || loc5 == "")) 
                    {
                        LoaderManager.instance.load(loc5, this.onPreLoaded, LoaderPriority.LevelC);
                    }
                    ++loc4;
                }
            }
            return;
        }

        /* internal  */onPreLoaded(arg1: ResourceInfo): void
        {
            return;
        }

        /* internal  */onLeaveCopyHandler(arg1: Object): void
        {
            if (!cache.copy.isInLv70DramaCopy()) 
            {
                return;
            }
            SceneRange.cancelNoMoveWH(false);
            Game.scene.isInLockScene = false;
            GameController.gameui.getGameui().showAllUI(true);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            Lv70DramaConfig.instance.curCopy = null;
            if (this._stepOperator != null) 
            {
                this._stepOperator.hideBonusView();
                this._stepOperator.hideShopEntrance();
                this._stepOperator.dispose();
            }
            GameProxy.copy.getDramaCopyInfo();
            return;
        }

        /* internal  */hasTodayDone(): Boolean
        {
            return cache.copy.getLv70DramaTodayDoneTimes() >= Lv70DramaController.MaxPassTimes;
        }

        /* internal  */stepSuccessHandler(arg1: Object): void
        {
            var loc1=int(arg1);
            this.stepOperator.stepSuccess(loc1);
            return;
        }

        /* internal  */nextStepHandler(arg1: Object): void
        {
            var loc5=null;
            var loc1=int(arg1);
            if (Global.isDebugModle) 
            {
                loc5 = "70级副本，服务器告诉前端执行步骤:" + loc1 + ",  现在的时间是:" + flash.utils.getTimer();
                Log.debug(loc5);
            }
            if (loc1 == 0) 
            {
                this.stepOperator.enteredDramaCopy(Lv70DramaStepOperator.LockOpAndViews);
                return;
            }
            var loc2=Lv70DramaConfig.instance.curCopy;
            if (loc2 == null) 
            {
                return;
            }
            var loc3=loc2.copyCode;
            var loc4;
            if ((loc4 = Lv70DramaConfig.instance.getDrama(loc3, loc1)) == null) 
            {
                return;
            }
            this.stepOperator.operate(loc4);
            return;
        }

        /* internal  */onLv70DramaTaskHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level < LevelLimitDefin.Lv70DramaLimitLevel) 
            {
                return;
            }
            if (this.hasTodayDone()) 
            {
                return;
            }
            this.icon.show();
            this.icon.showEffect(cache.guide.guideCopyStory);
            this.icon.showGuidArrow(cache.guide.guideCopyStory);
            if (cache.active.isActiveInEffect(EActiveType._EActiveTypeDramaReward)) 
            {
                this.icon.updateActiveToClientInfo(cache.active.getActiveByCode(EActiveType._EActiveTypeDramaReward));
            }
            else if (cache.active.isActiveInEffect(EActiveType._EActiveTypeDramaMultiDrop)) 
            {
                this.icon.updateActiveToClientInfo(cache.active.getActiveByCode(EActiveType._EActiveTypeDramaMultiDrop));
            }
            else 
            {
                this.icon.updateActiveToClientInfo(null);
            }
            GameProxy.copy.getDramaCopyInfo();
            return;
        }

        private /* const */MinCombatForDifficult: int=30000;

        private /* const */EasyChapterNum: int=3;

        public static /* const */MaxPassTimes: int=3;

        private /* var */_stepOperator: Lv70DramaStepOperator;

        private /* var */_winChoose: Lv70DramaChoosePanel;

        private /* var */_isResFileGot: Boolean=false;

        private /* var */_icon: Lv70DramaIconBtn;

        private /* var */_askInDramaCopy: TDramaCopy;

        private /* var */CurChapterNum: int=2;

        private /* var */_isLoading: Boolean=false;

        private /* var */_dramaCode: int;

        private /* var */codesForPreLoad: Object;

        private /* var */_dramaMultipPanel: DramaMultipleDelegatePanel;

        private /* var */_map: Lv70DramaMapMain;

        private /* var */_lastTab: int=1;
    }
